package proxy;

import api.Girl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author shenhan
 * @Date: 2020/1/16 20:37
 * @Description:
 */
public class WangMeiLiProxy implements InvocationHandler {
    private Girl girl;

    public WangMeiLiProxy(Girl girl) {
        this.girl = girl;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        dosomethingBefore();
        Object res = method.invoke(girl, args);
        dosomethingAfter();
        return res;
    }

    private void dosomethingAfter() {
        System.out.println("看看刚刚干啥了！");
    }

    private void dosomethingBefore() {
        System.out.println("调查一下背景！");
    }

    public Object getProxyInterface() {
        return Proxy.newProxyInstance(girl.getClass().getClassLoader(), girl.getClass().getInterfaces(), this);
    }
}
